<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;

class Citycategory extends Common
{

	//列表
    public function index()
    {
        $title = $this->request->get('title');
        $this->assign(array('title'=>$title));
        $where = [];
        if($title)
            $where['title'] = array('like','%'.$title.'%');
        $count = Db::table('cg_citycategory')->where($where)->count();
        $data = Db::table('cg_citycategory')->order("id desc")->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
         $maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['pusername'] = Db::table('users')->where(array('id'=>$v['puser']))->value('login_name');
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('maxpage',$maxpage);
        $this->assign('count',$count);
        return $this->fetch('index');
    }
    public function setcity()
    {
        $id = $this->request->get('id');
        $title = Db::table('cg_citycategory')->where(array('id'=>$id))->value('title');
        $this->assign('title',$title);
        $this->assign('id',$id);
        //查询没分组的城市
        $cities = Db::table('cg_city')->where('cid is null')->field('id,title')->select();
        $this->assign('cities',$cities);

        //查询该组的城市
        $gaizucitys = Db::table('cg_city')->where(array('cid'=>$id))->field('id,title')->select();
        $this->assign('gaizucitys',$gaizucitys);

        return $this->fetch();
    }
    public function add(){

        $validate = new Validate([
            'title'  => 'require|max:100|unique:cg_citycategory',
            'sort'  => 'require|max:11|unique:cg_citycategory'
        ]);
        $data = [];
        $data['title'] = $this->request->post('title');
        $data['brief'] = $this->request->post('brief');
        $data['sort'] = $this->request->post('sort');
        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }

        $data['cuser'] = $_SESSION['think']['employee_id'];
        $data['ctime'] = time();
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();
        $bool = Db::table('cg_citycategory')->insert($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"添加成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"添加失败"
            ]);die;
        }

    }

    public function update(){

        $id = $this->request->post('id');
        $data = [];
        $data['title'] = $this->request->post('title');
        $data['id'] = $id;
        $data['brief'] = $this->request->post('brief');
        $data['sort'] = $this->request->post('sort');
        $validate = new Validate([
            'title'  => 'require|max:100|unique:cg_citycategory',
            'id' => 'require|number',
            'sort'  => 'require|max:11|unique:cg_citycategory'
        ]);

        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }

        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();
        $bool = Db::table('cg_citycategory')->where(array('id'=>$id))->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"修改成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"修改失败"
            ]);die;
        }

    }

    public function delete(){
        $id = $this->request->post('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $bool = Db::table('cg_citycategory')->where(array('id'=>$id))->delete();
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }

    public function savecity(){

        $cities = $this->request->post('cities');
        $id = $this->request->post('id');
        if(!$cities){
            echo  json_encode([
                "success"=>false,
                "msg"=>'请选择城市'
            ]);die;
        }

        //先把改城市分类的所有城市的cid（城市分类id）设置为null，表示没有清除这些城市的分类
        Db::execute("update cg_city set cid = null where cid=?",[$id]);

        $data = [];
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();
        $data['cid'] = $id;
        $bool = Db::table('cg_city')->where("id in(".$cities.")")->update($data);

        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"设置成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"设置失败"
            ]);die;
        }

    }
}
